

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 一、参考资料 视频资料 二、运行环境  Windows11 Visual Studio Code v2022 Node.js v16.5.01 Vue&#x2F;cli v5.0.6 Bootstrap 5.1.3  三、页面跳转问题 参考文章：点击查看 3.1 MPA多页面 MPA，即 MultiPage Application，指由多个独立html页面的应用，每个页面需要重复加载">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署">
<meta property="og:url" content="http://example.com/2022/06/25/Vue2%20%E7%AC%94%E8%AE%B05%20Vue2%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC%E9%97%AE%E9%A2%98%20_%20vue-router%E8%B7%AF%E7%94%B1%E6%A6%82%E5%BF%B5%E3%80%81%E5%88%86%E7%B1%BB%E4%B8%8E%E4%BD%BF%E7%94%A8%20_%20%E7%BC%96%E7%A8%8B%E5%BC%8F%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA%20_%20%E8%B7%AF%E7%94%B1%E7%BB%84%E4%BB%B6%E7%9A%84%E7%BC%93%E5%AD%98%20_%205%E7%A7%8D%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB%20_%20%E5%B5%8C%E5%A5%97%E8%B7%AF%E7%94%B1%20_%20Vue2%E9%A1%B9%E7%9B%AE%E7%9A%84%E6%89%93%E5%8C%85%E4%B8%8E%E9%83%A8%E7%BD%B2/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 一、参考资料 视频资料 二、运行环境  Windows11 Visual Studio Code v2022 Node.js v16.5.01 Vue&#x2F;cli v5.0.6 Bootstrap 5.1.3  三、页面跳转问题 参考文章：点击查看 3.1 MPA多页面 MPA，即 MultiPage Application，指由多个独立html页面的应用，每个页面需要重复加载">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/b6d1d2ea621945829aae902e303668ab.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0ad043c1b97c4e1e94257570e4a3d7a1.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/76628fad3963400b8331b578fac5e6b5.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4c140ce1cc2e45c38bced7faffb51704.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d42a517c914a4df09a2f0f34b26f4741.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/965e976657da4521abbbc029fd0d96fd.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4a256fa23d954f2fb5432d6d45020cf2.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e071e321386d404a9b9690150a77910a.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2f245e23e0034eb9b0d44c7f2b277e3a.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/0efb75a1e89c40788e84e6617804d8b2.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/67791205c92a4aee98c5a2dd31b0e6c8.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/7025b653c2194210adc8fa19e998b9dd.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a0cf41e31e4a4f24910542dfbe604033.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/907bf122d74440b1b8716b3918d943f1.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/9572985ce25c4f2698341d8008642c4d.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/90a8f4160a374a178467b2fe65d09d24.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/be64b294be8b45bf9a7b57edf7b0e693.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ba22eca6807b4a66b03381dcfae736d7.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/37f78ca6b4e14eabb4fba24a14bfcaa6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/7d1edb743bf84851a063c8c07ec811fd.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/65a174f39eb24701ae88fdcc9815e40b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/b711b97ad7d54a33bf0da1766802144f.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/224de4a83eff4ec185c657e65b33ad68.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/589b8502fa6f43569bde671300c66ab6.png">
<meta property="article:published_time" content="2022-06-25T15:28:29.000Z">
<meta property="article:modified_time" content="2022-08-22T15:49:34.317Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/b6d1d2ea621945829aae902e303668ab.gif#pic_center">
  
  
  
  <title>Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-06-25 23:28" pubdate>
          2022年6月25日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          29k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          240 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="一、参考资料"><a href="#一、参考资料" class="headerlink" title="一、参考资料"></a>一、参考资料</h1><hr>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Zy4y1K7SH?p=118">视频资料</a></p>
<h1 id="二、运行环境"><a href="#二、运行环境" class="headerlink" title="二、运行环境"></a>二、运行环境</h1><hr>
<ul>
<li>Windows11</li>
<li>Visual Studio Code v2022</li>
<li>Node.js v16.5.01</li>
<li>Vue&#x2F;cli v5.0.6</li>
<li>Bootstrap 5.1.3</li>
</ul>
<h1 id="三、页面跳转问题"><a href="#三、页面跳转问题" class="headerlink" title="三、页面跳转问题"></a>三、页面跳转问题</h1><hr>
<p>参考文章：<a target="_blank" rel="noopener" href="https://juejin.cn/post/7018876571658223623">点击查看</a></p>
<h2 id="3-1-MPA多页面"><a href="#3-1-MPA多页面" class="headerlink" title="3.1 MPA多页面"></a>3.1 MPA多页面</h2><blockquote>
<p><strong>MPA</strong>，即 MultiPage Application，指由多个独立html页面的应用，每个页面需要重复加载js、css等资源，页面切换由HTML文档进行控制，返回结果是HTML文档。</p>
</blockquote>
<p>案例：index.html 和 about.html 两个静态页面之间的跳转，效果图如下：</p>
<p><img src="https://img-blog.csdnimg.cn/b6d1d2ea621945829aae902e303668ab.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>index.html</strong> </p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;IE=edge&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width,initial-scale=1.0&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;icon&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;&lt;%= BASE_URL %&gt;favicon.ico&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- CSS only --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>&lt;%= htmlWebpackPlugin.options.title %&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-offset-2 col-sm-8&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;page-header&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Vue2路由案例<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-sm-offset-2&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item active&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./index.html&quot;</span>&gt;</span>主页<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./about.html&quot;</span>&gt;</span>关于我们<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-6&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;panel&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-body&quot;</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>主页<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><strong>about.html</strong> </p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;IE=edge&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width,initial-scale=1.0&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;icon&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;&lt;%= BASE_URL %&gt;favicon.ico&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>&lt;%= htmlWebpackPlugin.options.title %&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- CSS only --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">noscript</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>We&#x27;re sorry but &lt;%= htmlWebpackPlugin.options.title %&gt; doesn&#x27;t work properly without JavaScript enabled. Please enable it to continue.<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">noscript</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-offset-2 col-sm-8&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;page-header&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Vue2路由案例<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-sm-offset-2&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./index.html&quot;</span>&gt;</span>主页<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item active&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./about.html&quot;</span>&gt;</span>关于我们<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-6&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;panel&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-body&quot;</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>关于我们<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br><br></code></pre></td></tr></table></figure>

<p><strong>特点：</strong> URL发生变化、全局页面重新加载。</p>
<p><strong>优点：</strong> </p>
<ul>
<li>首次加载速度快，访问页面时，服务器返回HTML，页面就会显示出来，整个过程只经历一次HTTP请求。</li>
<li>SEO效果好，搜索引擎在做网页排名时，会根据网页的内容来评判与排名。搜索引擎可是被HTML内容，多页面的应用则有更高的排名。</li>
</ul>
<p><strong>缺点：</strong></p>
<ul>
<li>页面切换速度慢，每次跳转都要发送一个HTTP请求，如果网络卡，那么页面跳转的时候就会发送卡顿，影响用户体验。</li>
<li>容易影响用户体验，如果网络慢，页面加载半天都加载不完，用户体验会很差。</li>
</ul>
<h2 id="3-2-SPA单页面"><a href="#3-2-SPA单页面" class="headerlink" title="3.2 SPA单页面"></a>3.2 SPA单页面</h2><blockquote>
<p><strong>SPA</strong>，即 Single Page Web Application，是指单页面的Web应用，别名 CSR（Client Side Render），即客户端渲染。它所需的资源，如HTML、CSS和JS等，在一次请求中就加载完成。<br>浏览器渲染是指所有的页面渲染、逻辑处理、页面路由、接口请求均是在浏览器中发生。</p>
</blockquote>
<blockquote>
<p>对于SPA来说，页面的切换就是组件或视图之间的切换。<br>简单地说，SPA应用程序只有单个HTML页面文件，在Vue中可通过vue-router插件来局部切换组件，而不用刷新整个页面，从而实现无刷新切换页面。</p>
</blockquote>
<p><strong>SPA原理</strong></p>
<p>参考资料：<a target="_blank" rel="noopener" href="https://wenku.baidu.com/view/44320ebf52e79b89680203d8ce2f0066f533643b.html">点击查看</a></p>
<blockquote>
<p>SPA的重要实现之一就是改变路由时，页面不刷新。实现这个功能通常有两种方式，分别是适用 window.history对象 或 location.hash。</p>
</blockquote>
<blockquote>
<p>window.history 包含了浏览器的历史信息，常用的方法有：</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js">history.<span class="hljs-title function_">back</span>() <span class="hljs-comment">// 相当于在浏览器点击后退按钮</span><br>history.<span class="hljs-title function_">forward</span>() <span class="hljs-comment">// 相当于在浏览器点击前进按钮</span><br>history.<span class="hljs-title function_">go</span>(n) 		<span class="hljs-comment">// 接收一个整数作为参数，移动到该整数指定的页面，比如go(1)相当于forawrd()，go(-1)则相当于back()，go(0)相当于刷新当前页面。</span><br></code></pre></td></tr></table></figure>

<blockquote>
<p>HTML5 对 history对象新增了 pushState() 和 replaceState() 方法，这两个方法可以往历史栈中添加数据，给用户的感觉就是浏览器的URL改变了，但是页面并没有重新刷新，<code>pushState()</code> 是在浏览器记录中添加一个新的记录，<code>replaceState()</code> 则是修改当前的浏览器记录，这是二者的差别，除此之外。使用时参数的字段和含义都一样。</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">window</span>.<span class="hljs-property">history</span>.<span class="hljs-title function_">pushState</span>(state, title, url)<br></code></pre></td></tr></table></figure>
<blockquote>
<p>hash 是 location对象的属性，它是指URL的锚，即从#号开始的部分，修改location.hash并监听 window。hashchange事件也能达到同样的效果。</p>
</blockquote>
<p>注：vue-router插件中Vue不支持 onHashChange事件，需要vue-router插件里的钩子函数解决。</p>
<p><strong>特点：</strong> URL发生变化、整体页面不用重新加载，局部页面发生变化。</p>
<p><strong>优点：</strong> </p>
<ul>
<li>页面切换速度快，由于页面每次切换时，不需要HTML文件的请求，这样节省了很多http发送时延。</li>
<li>用户体验极佳，页面局部切换快，尤其是移动设备，尽管在网络环境查，由于组件是预先加载好的，所以不用发网络请求，使得用户有更好的视觉体验。</li>
<li>分离前后端关注点，前端负责界面显示，后端负责数据存储和计算，各尽其职，不会混乱。</li>
<li>减轻服务器的压力，服务器只需要出数据，不用管展示的逻辑和页面合成，提高了吞吐量。</li>
<li>API共享，同一套后端程序代码，不用修改就可以用于Web界面、手机、平板等多种客户端。</li>
<li>完全的前端组件化，前端开发不再以页面为单位，更多地采用组件化的思想，代码结构和组织方式更加规范化，便于修改和调整。</li>
</ul>
<p><strong>缺点：</strong> </p>
<ul>
<li>首次加载速度慢，首次加载需请求一次HTML，同时还要发送若干次JS请求，相当于MPA会慢一点。</li>
<li>SEO效果差，因为搜索引擎只识别HTML的内容，不识别JS，而单页面应用内容都是靠JS代码渲染出来的，搜索引擎不识别这部分内容，固然不会给好的排名，这就导致SPA应用做出来的网页在各大搜索引擎，比如谷歌、百度等上面的排名较低。</li>
<li>较高的前端开发门槛，对技术能力要求较高，需对设计模式有一定的理解，因为面对的不只是简单的页面，而是一个运行在浏览器里面的应用。</li>
</ul>
<h2 id="3-3-MPA比较SPA"><a href="#3-3-MPA比较SPA" class="headerlink" title="3.3 MPA比较SPA"></a>3.3 MPA比较SPA</h2><p>参考：<a target="_blank" rel="noopener" href="https://juejin.cn/post/7018876571658223623">点击查看</a></p>
<table>
<thead>
<tr>
<th></th>
<th align="left">单页面应用 SPA</th>
<th align="left">多页面应用 MPA</th>
</tr>
</thead>
<tbody><tr>
<td>组成</td>
<td align="left">一个外壳页面和多个页面片段组成</td>
<td align="left">多个完整页面构成</td>
</tr>
<tr>
<td>跳转方式</td>
<td align="left">单个页面片段删除或隐藏，加载另一个页面片段并显示。片段之间模拟跳转，不离开外壳页面。</td>
<td align="left">页面之间的跳转是从一个页面到另一个页面</td>
</tr>
<tr>
<td>资源公用性（CSS和JS）</td>
<td align="left">公用，只需在外壳页面加载</td>
<td align="left">不共用，每个页面要单独加载</td>
</tr>
<tr>
<td>刷新方式</td>
<td align="left">页面局部刷新或更改</td>
<td align="left">整个页面进行刷新</td>
</tr>
<tr>
<td>URL模式</td>
<td align="left">xxx.com&#x2F;#&#x2F;page1，xxx.com&#x2F;#&#x2F;page2</td>
<td align="left">xxx.com&#x2F;page1.html ，xxx.com&#x2F;page2.html</td>
</tr>
<tr>
<td>用户体验感</td>
<td align="left">页面切换快，用户体验好（包括移动设备）</td>
<td align="left">页面切换加载慢，用户体验差，尤其是在移动设备</td>
</tr>
<tr>
<td>转场动画</td>
<td align="left">易实现</td>
<td align="left">无法实现</td>
</tr>
<tr>
<td>页面之间的数据传递</td>
<td align="left">容易（Vuex或Vue中的父子组件通信</td>
<td align="left">困难，依赖URL传参，或Cookie、localStorage和IndexDB等</td>
</tr>
<tr>
<td>搜索引擎优化（SEO）</td>
<td align="left">困难，可采取服务器端渲染（SSR）优化</td>
<td align="left">容易实现</td>
</tr>
<tr>
<td>适用范围</td>
<td align="left">高体验需求、追求界面流畅度的应用</td>
<td align="left">适用于追求高度支持搜索引擎的应用</td>
</tr>
<tr>
<td>开发成本</td>
<td align="left">较高，需借助专业的框架，如React、Vue等来降低开发难度</td>
<td align="left">较低，但页面重复的代码多。</td>
</tr>
<tr>
<td>维护成本</td>
<td align="left">相对较低</td>
<td align="left">相对较高</td>
</tr>
</tbody></table>
<h2 id="3-4-SPA总结"><a href="#3-4-SPA总结" class="headerlink" title="3.4 SPA总结"></a>3.4 SPA总结</h2><p>在Vue开发中，使用的就是SPA单页面开发的设计模式。</p>
<p>SPA的特点再次总结如下：</p>
<ul>
<li>SPA，Single Page Web Application，指单页Web应用。</li>
<li>整个应用只有一个完整的页面。</li>
<li>点击页面中的导航链接不会刷新页面，只会做页面的局部更新。</li>
<li>部分数据需要通过 ajax请求获取。</li>
</ul>
<h1 id="四、vue-router-路由插件"><a href="#四、vue-router-路由插件" class="headerlink" title="四、vue-router 路由插件"></a>四、vue-router 路由插件</h1><hr>
<h2 id="4-1-路由的理解"><a href="#4-1-路由的理解" class="headerlink" title="4.1 路由的理解"></a>4.1 路由的理解</h2><p>路由的特点：</p>
<ol>
<li>一个路由就是一组映射关系（key - value）</li>
<li>key 为路径，value 可能是 function 或者 component 组件</li>
</ol>
<h2 id="4-2-路由分类"><a href="#4-2-路由分类" class="headerlink" title="4.2 路由分类"></a>4.2 路由分类</h2><h3 id="4-2-1-后端路由"><a href="#4-2-1-后端路由" class="headerlink" title="4.2.1 后端路由"></a>4.2.1 后端路由</h3><p>value是 function 函数，用于处理客户端提交的请求</p>
<p>工作过程：服务器接收到一个请求时，根据请求路径找到匹配的函数来处理请求，返回响应数据。</p>
<h3 id="4-2-2-前端路由"><a href="#4-2-2-前端路由" class="headerlink" title="4.2.2 前端路由"></a>4.2.2 前端路由</h3><p>value是component，用于展示页面内容</p>
<p>工作过程：当浏览器的路径改变时，对应的组件就会显示。</p>
<h2 id="4-3-Vue2路由的使用"><a href="#4-3-Vue2路由的使用" class="headerlink" title="4.3 Vue2路由的使用"></a>4.3 Vue2路由的使用</h2><p><a target="_blank" rel="noopener" href="https://v3.router.vuejs.org/zh/"><strong>vue-router</strong></a></p>
<p>包含功能：</p>
<ul>
<li>嵌套的路由&#x2F;视图表</li>
<li>模块化的、基于组件的路由配置</li>
<li>路由参数、查询、通配符</li>
<li>基于 Vue.js 过渡系统的视图过渡效果</li>
<li>细粒度的导航控制</li>
<li>带有自动激活的 CSS class 的链接</li>
<li>HTML5 历史模式或 hash 模式</li>
<li>自定义的滚动条行为</li>
</ul>
<p>Vue2版本目前可以使用的最新路由插件是 vue-router@3，而Vue3则可以使用最新的vue-route@4</p>
<p><strong>第一步，在Vue脚手架项目中安装vue-router@3插件</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">npm i vue-router@3 <br></code></pre></td></tr></table></figure>
<p><strong>第二步，将路由组件放到单独的文件夹，比如pages文件夹</strong></p>
<p><img src="https://img-blog.csdnimg.cn/0ad043c1b97c4e1e94257570e4a3d7a1.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>About.vue</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>About组件的内容<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;About&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">beforeDestroy</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;About组件即将被销毁&#x27;</span>)</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><code>Home.vue</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Home组件的内容<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Home&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">beforeDestroy</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;Home组件即将被销毁&#x27;</span>)</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><strong>第三步，创建router文件夹和index.js，设置路由</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">VueRouter</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vue-router&quot;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">About</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../pages/About&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Home</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../pages/Home&#x27;</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span>(&#123;<br>    <span class="hljs-attr">routes</span>: [<br>        &#123;<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/about&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">About</span>,<br>        &#125;,<br>        &#123;<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/home&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">Home</span><br>        &#125;<br>    ]<br>&#125;)<br></code></pre></td></tr></table></figure>
<p><strong>第四步，App组件，SPA单页面核心，使用路由的标签</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-offset-2 col-sm-8&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;page-header&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Vue2路由案例<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-sm-offset-2&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span><br>            <span class="hljs-comment">&lt;!-- Vue中借助router-link标签实现路由的切换 --&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">active-class</span>=<span class="hljs-string">&quot;active&quot;</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/home&quot;</span>&gt;</span>首页<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">active-class</span>=<span class="hljs-string">&quot;active&quot;</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/about&quot;</span>&gt;</span>关于我们<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-6&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;panel&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-body&quot;</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">router-view</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-view</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;App&#x27;</span>,</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>在上述中，使用到路由的部分如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link <span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> active-<span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;active&quot;</span> to=<span class="hljs-string">&quot;/home&quot;</span>&gt;<br>vue-router插件提供的标签, 类似于a标签， active-<span class="hljs-keyword">class</span>指定为选中后的标签，to指定跳转的组件映射路径<br><br>&lt;router-view&gt;&lt;/router-view&gt;<br>用于标记路由组件的显示位置<br></code></pre></td></tr></table></figure>




<p><strong>实现效果：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/76628fad3963400b8331b578fac5e6b5.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-4-Vue2路由使用的注意点"><a href="#4-4-Vue2路由使用的注意点" class="headerlink" title="4.4 Vue2路由使用的注意点"></a>4.4 Vue2路由使用的注意点</h2><ol>
<li>路由组件通过 <code>&lt;route-link&gt;</code> 标签引入，通过 <code>&lt;route-view&gt;</code>标签显示。路由组件不需要在父组件中引入，直接通过路由标签中的to属性指定映射的path即可。</li>
<li>在路由组件发生切换的时候，当前位置原先使用<code>&lt;route-view&gt;</code> 渲染出来的路由组件会被直接销毁（通过 beforeDestory()钩子函数可以验证），需要的时候再进行挂载。</li>
<li>路由组件对象都包含<code>$router</code> 和 <code>$route</code>，其中 <code>$router</code>都是同一个，而 <code>$route</code> 是各个路由组件各不相同的。</li>
</ol>
<p>比如：上个案例的Home组件的this输出如下：</p>
<p><img src="https://img-blog.csdnimg.cn/4c140ce1cc2e45c38bced7faffb51704.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>而About组件的this输出如下：<br><img src="https://img-blog.csdnimg.cn/d42a517c914a4df09a2f0f34b26f4741.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>光是肉眼判断并不能说明这个结论完全正确，接下来借助window的对象来验证。（基于4.3的案例）<br><strong>Home.vue</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title function_">monted</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-variable language_">window</span>.<span class="hljs-property">homeRouter</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">$router</span><br>	<span class="hljs-variable language_">window</span>.<span class="hljs-property">homeRoute</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">$route</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p><strong>About.vue</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title function_">monted</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-variable language_">window</span>.<span class="hljs-property">aboutRouter</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">$router</span><br>	<span class="hljs-variable language_">window</span>.<span class="hljs-property">aboutRoute</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">$route</span><br>&#125;<br></code></pre></td></tr></table></figure>
<p>由于是路由组件，所以在测试的时候，需要两个路由组件都出现过，然后在浏览器控制台进行判断，结果如下：<br><img src="https://img-blog.csdnimg.cn/965e976657da4521abbbc029fd0d96fd.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>结论：所有的路由组件所带的 <code>$router</code> 都是相同的</p>
<ol start="4">
<li>引入路由组件的父组件也含有 <code>$router</code> 和 <code>$route</code> ，其中<code>$route</code> 则是当前页面中显示的路由的配置信息。<br><img src="https://img-blog.csdnimg.cn/4a256fa23d954f2fb5432d6d45020cf2.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
<li>路由组件通常放在 pages 文件夹，一般的组件通常放在 components文件夹</li>
<li>整个Vue应用只有一个路由器，可以通过组件的 <code>$router</code> 属性获取</li>
</ol>
<h2 id="4-5-多级嵌套路由"><a href="#4-5-多级嵌套路由" class="headerlink" title="4.5 多级嵌套路由"></a>4.5 多级嵌套路由</h2><p><a target="_blank" rel="noopener" href="https://router.vuejs.org/zh/guide/essentials/nested-routes.html"><strong>嵌套路由</strong></a></p>
<p>这里先通过两个普通的HTML页面来体现这种多级的路由效果：<br>(about.html 和 index.html 可以参考4.3的案例）</p>
<p><img src="https://img-blog.csdnimg.cn/e071e321386d404a9b9690150a77910a.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><code>about-message.html</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;IE=edge&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width,initial-scale=1.0&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;icon&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;&lt;%= BASE_URL %&gt;favicon.ico&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- CSS only --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>&lt;%= htmlWebpackPlugin.options.title %&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-offset-2 col-sm-8&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;page-header&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Vue2多级路由案例<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-sm-offset-2&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./index.html&quot;</span>&gt;</span>主页<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item active&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./about.html&quot;</span>&gt;</span>关于我们<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-6&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;panel&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-body&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>About组件的内容<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;nav nav-tabs&quot;</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> <br>                        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./home-news.html&quot;</span>&gt;</span>新闻<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> <br>                        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item active&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./home-message.html&quot;</span>&gt;</span>消息<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/message1&quot;</span>&gt;</span>message1<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/message2&quot;</span>&gt;</span>message2<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/message3&quot;</span>&gt;</span>message3<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><code>about-news.html</code></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;IE=edge&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width,initial-scale=1.0&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;icon&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;&lt;%= BASE_URL %&gt;favicon.ico&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- CSS only --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>&lt;%= htmlWebpackPlugin.options.title %&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-offset-2 col-sm-8&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;page-header&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Vue2多级路由案例<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-sm-offset-2&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./index.html&quot;</span>&gt;</span>主页<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item active&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./about.html&quot;</span>&gt;</span>关于我们<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-6&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;panel&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-body&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>About组件的内容<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;nav nav-tabs&quot;</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> <br>                                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item active&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./home-news.html&quot;</span>&gt;</span>新闻<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> <br>                                <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;./home-message.html&quot;</span>&gt;</span>消息<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>                            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                        <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>                                news1<br>                            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>                                news2<br>                            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>                                news3<br>                            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>                          <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>                  <br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>使用Vue2 基于 vue-router 实现，效果图如下：</p>
<p><img src="https://img-blog.csdnimg.cn/2f245e23e0034eb9b0d44c7f2b277e3a.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>项目结构：<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/a80ad1c65ebc47dfb2798aa7cb4ed029.png#">https://img-blog.csdnimg.cn/a80ad1c65ebc47dfb2798aa7cb4ed029.png#</a> &#x3D;50%x)<br><strong>1. main.js</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">App</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./App.vue&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">VueRouter</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue-router&#x27;</span><br><span class="hljs-keyword">import</span> router <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./router&#x27;</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">VueRouter</span>)<br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>  <span class="hljs-attr">el</span>: <span class="hljs-string">&#x27;#app&#x27;</span>,<br>  router,<br>  <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>
<p><strong>2. public&#x2F;index.html</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;IE=edge&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width,initial-scale=1.0&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;icon&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;&lt;%= BASE_URL %&gt;favicon.ico&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>&lt;%= htmlWebpackPlugin.options.title %&gt;<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">noscript</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>We&#x27;re sorry but &lt;%= htmlWebpackPlugin.options.title %&gt; doesn&#x27;t work properly without JavaScript enabled. Please enable it to continue.<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">noscript</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><strong>3. App.vue</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-offset-2 col-sm-8&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;page-header&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Vue2多级路由案例<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-sm-offset-2&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span><br>            <span class="hljs-comment">&lt;!-- Vue中借助router-link标签实现路由的切换 --&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">active-class</span>=<span class="hljs-string">&quot;active&quot;</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/home&quot;</span>&gt;</span>首页<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">active-class</span>=<span class="hljs-string">&quot;active&quot;</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/about&quot;</span>&gt;</span>关于我们<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-6&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;panel&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;panel-body&quot;</span>&gt;</span><br>                  <span class="hljs-tag">&lt;<span class="hljs-name">router-view</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-view</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;App&#x27;</span>,</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>4.  About.vue</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>     <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;nav nav-tabs&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> <br>            <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">active-class</span>=<span class="hljs-string">&quot;active&quot;</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/about/news&quot;</span>&gt;</span>新闻<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> <br>            <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">active-class</span>=<span class="hljs-string">&quot;active&quot;</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/about/message&quot;</span>&gt;</span>消息<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">router-view</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-view</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;About&#x27;</span>,</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>5. Home.vue</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Home组件的内容<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Home&#x27;</span>,</span><br><span class="language-javascript"></span><br><span class="language-javascript">    <span class="hljs-title function_">beforeDestroy</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;Home组件即将被销毁&#x27;</span>)</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>6. AboutMessage.vue</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/message1&quot;</span>&gt;</span>message1<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/message2&quot;</span>&gt;</span>message2<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;/message3&quot;</span>&gt;</span>message3<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;AboutMessage&#x27;</span>,</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>7. AboutNews.vue</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>news1<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>news2<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>news3<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>         <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;AboutNews&#x27;</span>,</span><br><span class="language-javascript">    </span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>8. router&#x2F;index.js</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> <span class="hljs-title class_">VueRouter</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vue-router&quot;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">About</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../pages/About&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Home</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../pages/Home&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">AboutMessage</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../pages/AboutMessage&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">AboutNews</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;../pages/AboutNews&#x27;</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span>(&#123;<br>    <span class="hljs-attr">routes</span>: [<br>        &#123;<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/about&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">About</span>,<br>            <span class="hljs-attr">children</span>: [<br>                &#123;<br>                    <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;message&#x27;</span>,<br>                    <span class="hljs-attr">component</span>: <span class="hljs-title class_">AboutMessage</span>,<br>                &#125;,<br>                &#123;<br>                    <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;news&#x27;</span>,<br>                    <span class="hljs-attr">component</span>: <span class="hljs-title class_">AboutNews</span>,<br>                &#125;<br>            ]<br>        &#125;,<br>        &#123;<br>            <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/home&#x27;</span>,<br>            <span class="hljs-attr">component</span>: <span class="hljs-title class_">Home</span><br>        &#125;<br>    ]<br>&#125;)<br></code></pre></td></tr></table></figure>
<p>使用总结：</p>
<ul>
<li>在routes对应路由里面添加数组类型的chilren，每个路由至少要设置 path 和 coponent 两个属性</li>
<li>引入通过<code>&lt;route-link active-class=&quot;响应式的样式类&quot; to=&quot;/一级路由/二级路由&quot;&gt;</code></li>
<li>插入位置同样是使用 <code>&lt;route-view&gt;&lt;/route-view&gt;</code> 指定</li>
<li>使用路由组件时，只需要在路由设置的JS文件里导入组件即可，其他父组件不用再次引入路由组件。</li>
</ul>
<h2 id="4-6-vue-router路由传参"><a href="#4-6-vue-router路由传参" class="headerlink" title="4.6 vue-router路由传参"></a>4.6 vue-router路由传参</h2><h3 id="4-6-1-query传参"><a href="#4-6-1-query传参" class="headerlink" title="4.6.1 query传参"></a>4.6.1 query传参</h3><p>通过打印路由组件可以发现其中的$route对象含有 query这个属性，该属性则包含了在引入路由时候传递的参数值。<br><img src="https://img-blog.csdnimg.cn/0efb75a1e89c40788e84e6617804d8b2.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>案例：</strong> 使用路由传参</p>
<p><img src="https://img-blog.csdnimg.cn/67791205c92a4aee98c5a2dd31b0e6c8.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>在 4.5 案例的基础上添加一个消息详情的组件，同时增加该组件嵌套的路由。</p>
<p><strong>1. MessageDetail.vue</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;shadow&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>消息编号: &#123;&#123;$route.query.id&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>消息标题: &#123;&#123;$route.query.title&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;MessageDetail&#x27;</span></span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>2. router&#x2F;index.js</strong> 关键修改部分：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;message&#x27;</span>,<br><span class="hljs-attr">component</span>: <span class="hljs-title class_">AboutMessage</span>,<br><span class="hljs-attr">children</span>: [<br>    &#123;<br>        <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;detail&#x27;</span>,<br>        <span class="hljs-attr">component</span>: <span class="hljs-title class_">MessageDetail</span><br>    &#125;<br>]<br></code></pre></td></tr></table></figure>

<p>引入消息详情的组件主要是身为二级路由的消息组件，所以相比于4.5案例，它需要做一些变动：<br><strong>3. AboutMessage.vue</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>   <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(msg) in messageList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;msg.id&quot;</span>&gt;</span><br>            <span class="hljs-comment">&lt;!-- 跳转路由并携带query参数, to的字符串写法--&gt;</span><br>            <span class="hljs-comment">&lt;!-- &lt;router-link active-class=&quot;active&quot; :to=&quot;`/about/message/detail?id=$&#123;msg.id&#125;&amp;title=$&#123;msg.title&#125;`&quot;&gt;&#123;&#123; msg.title &#125;&#125;&lt;/router-link&gt; --&gt;</span><br>            <span class="hljs-comment">&lt;!-- 跳转路由并携带query参数, to的对象写法--&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">:to</span>=<span class="hljs-string">&quot;&#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">                path: &#x27;/about/message/detail&#x27;,</span></span><br><span class="hljs-string"><span class="hljs-tag">                query: &#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">                    id: msg.id,</span></span><br><span class="hljs-string"><span class="hljs-tag">                    title: msg.title</span></span><br><span class="hljs-string"><span class="hljs-tag">                &#125;</span></span><br><span class="hljs-string"><span class="hljs-tag">            &#125;&quot;</span>&gt;</span> &#123;&#123;msg.title&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">router-view</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-view</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;AboutMessage&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> &#123;</span><br><span class="language-javascript">            <span class="hljs-attr">messageList</span>: [</span><br><span class="language-javascript">                &#123;<span class="hljs-attr">id</span>:<span class="hljs-string">&#x27;1&#x27;</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;高考分数线持续出炉&#x27;</span>&#125;,</span><br><span class="language-javascript">                &#123;<span class="hljs-attr">id</span>:<span class="hljs-string">&#x27;2&#x27;</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;热炸了？河南居民家中玻璃一碰即碎&#x27;</span>&#125;,</span><br><span class="language-javascript">                &#123;<span class="hljs-attr">id</span>:<span class="hljs-string">&#x27;3&#x27;</span>, <span class="hljs-attr">title</span>: <span class="hljs-string">&#x27;2022明星高考成绩&#x27;</span>&#125;,</span><br><span class="language-javascript">            ]</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>)</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>使用总结：</strong></p>
<ul>
<li><code>$route</code> 里的query读取的是URL中?后的参数，参数之间由&amp;分隔开</li>
<li>路由query的URL传参有两种方式，第一种是模板语法，不推荐：<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link active-<span class="hljs-keyword">class</span>=<span class="hljs-string">&quot;active&quot;</span> :to=<span class="hljs-string">&quot;`/about/message/detail?id=$&#123;msg.id&#125;&amp;title=$&#123;msg.title&#125;`&quot;</span>&gt;&#123;&#123; msg.<span class="hljs-property">title</span> &#125;&#125;&lt;/router-link&gt;<br></code></pre></td></tr></table></figure></li>
</ul>
<p>第二种是对象语法，推荐使用：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;router-link :to=<span class="hljs-string">&quot;&#123;</span><br><span class="hljs-string">    path: &#x27;/about/message/detail&#x27;,</span><br><span class="hljs-string">    query: &#123;</span><br><span class="hljs-string">        id: msg.id,</span><br><span class="hljs-string">        title: msg.title</span><br><span class="hljs-string">    &#125;</span><br><span class="hljs-string">&#125;&quot;</span>&gt;&lt;/router-link&gt;<br></code></pre></td></tr></table></figure>

<h3 id="4-6-2-params传参"><a href="#4-6-2-params传参" class="headerlink" title="4.6.2 params传参"></a>4.6.2 params传参</h3><p>params对象也存在于 $route中，它也是通过URL进行传参的，但是路由的path和引入路由的<code>&lt;route-link&gt;</code>时的<code>:to</code> 都有一定的限制，例如：</p>
<p>路由要使用<code>:参数名</code> 来表示参数，而且多个参数之间要用 <code>/</code> 隔开<br><strong>router&#x2F;index.js 路由的配置</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;message&#x27;</span>,<br><span class="hljs-attr">component</span>: <span class="hljs-title class_">AboutMessage</span>,<br><span class="hljs-attr">children</span>: [<br>   &#123;<br>       <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;msgDetail&#x27;</span>,<br>       <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;detail/:id/:title&#x27;</span>,<br>       <span class="hljs-attr">component</span>: <span class="hljs-title class_">MessageDetail</span><br>   &#125;<br>]<br></code></pre></td></tr></table></figure>
<p><strong>引入路由时：</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 法一: 字符串写法--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">:to</span>=<span class="hljs-string">&quot;`/about/message/detail/$&#123;msg.id&#125;/$&#123;msg.title&#125;`&quot;</span>&gt;</span> &#123;&#123;msg.title&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- 法二: 对象写法 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">:to</span>=<span class="hljs-string">&quot;&#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">    // 对象写法不能用path, 只能用name指定</span></span><br><span class="hljs-string"><span class="hljs-tag">    name: &#x27;msgDetail&#x27;,</span></span><br><span class="hljs-string"><span class="hljs-tag">    params: &#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">        id: msg.id,</span></span><br><span class="hljs-string"><span class="hljs-tag">        title: msg.title</span></span><br><span class="hljs-string"><span class="hljs-tag">    &#125;</span></span><br><span class="hljs-string"><span class="hljs-tag">&#125;&quot;</span>&gt;</span> &#123;&#123;msg.title&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>路由组件参数显示：</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;shadow&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>消息编号: &#123;&#123;$route.params.id&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>消息标题: &#123;&#123;$route.params.title&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;MessageDetail&#x27;</span></span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>运行效果：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/7025b653c2194210adc8fa19e998b9dd.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>使用总结：</strong></p>
<ul>
<li>路由携带 params参数时，若使用to的对象写法，则不能使用path配置项，必须使用name配置。</li>
</ul>
<h3 id="4-6-3-props配置传参"><a href="#4-6-3-props配置传参" class="headerlink" title="4.6.3 props配置传参"></a>4.6.3 props配置传参</h3><p>作用：让路由组件更方便的受到参数</p>
<p>路由的配置：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;message&#x27;</span>,<br><span class="hljs-attr">component</span>: <span class="hljs-title class_">AboutMessage</span>,<br><span class="hljs-attr">children</span>: [<br>   &#123;<br>       <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;msgDetail&#x27;</span>,<br>       <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;detail/:id/:title&#x27;</span>,<br>       <span class="hljs-attr">component</span>: <span class="hljs-title class_">MessageDetail</span>,<br>       <span class="hljs-comment">// 写法一: props值为对象，该对象中所有的key-value的组合最终都会通过props传给Detail组件</span><br>       <span class="hljs-comment">// props: &#123;id:1, title: &#x27;消息1&#x27;&#125;</span><br>       <br>       <span class="hljs-comment">// 写法二: props值为布尔值, 若为true，那么则把路由收到的所有params参数通过props传给Detail组件</span><br>       <span class="hljs-comment">// props: true</span><br><br>		<span class="hljs-comment">// 【推荐】写法三: props值为函数, 该函数返回的对象中每一组key-value都会通过props传给Detail组件</span><br>		<span class="hljs-title function_">props</span>(<span class="hljs-params">route</span>)&#123;<br>			<span class="hljs-keyword">return</span> &#123;<br>				<span class="hljs-attr">id</span>: route.<span class="hljs-property">query</span>.<span class="hljs-property">id</span>,<br>				<span class="hljs-attr">title</span>: route.<span class="hljs-property">query</span>.<span class="hljs-property">title</span><br>			&#125;<br>		&#125; <br>   &#125;<br>]<br></code></pre></td></tr></table></figure>
<h2 id="4-7-命名路由"><a href="#4-7-命名路由" class="headerlink" title="4.7 命名路由"></a>4.7 命名路由</h2><p>vue-router 支持的路由命令可以更方便地定位到路由，在配置路由时候指定name属性即可，需要注意的是，所有的路由名称都不能重复。</p>
<p><strong>命名前：</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">routes</span>: [<br>    &#123;<br>        <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/about&#x27;</span>,<br>        <span class="hljs-attr">component</span>: <span class="hljs-title class_">About</span>,<br>        <span class="hljs-attr">children</span>: [<br>            &#123;<br>                <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;message&#x27;</span>,<br>                <span class="hljs-attr">component</span>: <span class="hljs-title class_">AboutMessage</span>,<br>                <span class="hljs-attr">children</span>: [<br>                    &#123;<br>                        <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;detail&#x27;</span>,<br>                        <span class="hljs-attr">component</span>: <span class="hljs-title class_">MessageDetail</span><br>                    &#125;<br>                ]<br>            &#125;<br>        ]<br>    &#125;<br>]<br></code></pre></td></tr></table></figure>

<p><strong>命名前的路由指定：</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(msg) in messageList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;msg.id&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 跳转路由并携带query参数, to的字符串写法--&gt;</span><br>    <span class="hljs-comment">&lt;!-- &lt;router-link active-class=&quot;active&quot; :to=&quot;`/about/message/detail?id=$&#123;msg.id&#125;&amp;title=$&#123;msg.title&#125;`&quot;&gt;&#123;&#123; msg.title &#125;&#125;&lt;/router-link&gt; --&gt;</span><br>    <span class="hljs-comment">&lt;!-- 跳转路由并携带query参数, to的对象写法--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">:to</span>=<span class="hljs-string">&quot;&#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">        path: &#x27;/about/message/detail&#x27;,</span></span><br><span class="hljs-string"><span class="hljs-tag">        query: &#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">            id: msg.id,</span></span><br><span class="hljs-string"><span class="hljs-tag">            title: msg.title</span></span><br><span class="hljs-string"><span class="hljs-tag">        &#125;</span></span><br><span class="hljs-string"><span class="hljs-tag">    &#125;&quot;</span>&gt;</span> &#123;&#123;msg.title&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>命名后：（可以选择性的命名）</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">routes</span>: [<br>    &#123;<br>        <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/about&#x27;</span>,<br>        <span class="hljs-attr">component</span>: <span class="hljs-title class_">About</span>,<br>        <span class="hljs-attr">children</span>: [<br>            &#123;<br>                <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;message&#x27;</span>,<br>                <span class="hljs-attr">component</span>: <span class="hljs-title class_">AboutMessage</span>,<br>                <span class="hljs-attr">children</span>: [<br>                    &#123;<br>                    	<span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;msgDetail&#x27;</span>,<br>                        <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;detail&#x27;</span>,<br>                        <span class="hljs-attr">component</span>: <span class="hljs-title class_">MessageDetail</span><br>                    &#125;<br>                ]<br>            &#125;<br>        ]<br>    &#125;<br>]<br></code></pre></td></tr></table></figure>
<p><strong><a target="_blank" rel="noopener" href="https://v3.router.vuejs.org/zh/guide/essentials/named-routes.html#%E5%91%BD%E5%90%8D%E8%B7%AF%E7%94%B1">命名路由</a></strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">router-link</span> <span class="hljs-attr">:to</span>=<span class="hljs-string">&quot;&#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">   name: &#x27;msgDetail&#x27;,</span></span><br><span class="hljs-string"><span class="hljs-tag">   query: &#123;</span></span><br><span class="hljs-string"><span class="hljs-tag">       id: msg.id,</span></span><br><span class="hljs-string"><span class="hljs-tag">       title: msg.title</span></span><br><span class="hljs-string"><span class="hljs-tag">   &#125;</span></span><br><span class="hljs-string"><span class="hljs-tag">&#125;&quot;</span>&gt;</span> &#123;&#123;msg.title&#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">router-link</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>使用总结：</strong></p>
<ul>
<li>命名路由可以给路由指定name属性，设置路由时可直接通过name定位到路由</li>
<li>通过name定位的路由跳转的位置仍然是path属性，不会受到影响</li>
</ul>
<h2 id="4-8-replace属性"><a href="#4-8-replace属性" class="headerlink" title="4.8 replace属性"></a>4.8 replace属性</h2><p><strong>作用：</strong> 控制路由跳转时操作浏览器历史记录的模式</p>
<p><strong>使用方式：</strong> 在引用路由时候可以给<code>&lt;router-link&gt;</code> 添加如下属性：</p>
<p><code>replace=&quot;true&quot;</code> ，可以简写为<code>replace</code></p>
<p><strong>案例：设置replace前后，页面后退效果的对比</strong></p>
<p><strong>设置replace前：</strong><br><img src="https://img-blog.csdnimg.cn/a0cf41e31e4a4f24910542dfbe604033.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>路由的页面后退默认是后退到上一个访问到的页面，这里我访问了多次同一个组件里的不同路由组件，后退时会在不同的路由组件来回切换。</p>
<p><strong>设置replace后：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/907bf122d74440b1b8716b3918d943f1.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>添加 replace属性后，尽管点击过很多次跳转到路由组件的链接，但只要一后退，就会返回到上一层路由的网址，这就是replace属性的作用。</p>
<blockquote>
<p>浏览器的历史记录写入方式有两种：<br>1）push方式，追加历史记录<br>2）replace方式，替换当前记录</p>
</blockquote>
<p>vue-router 默认为push方式。</p>
<h2 id="4-9-编程式路由导航"><a href="#4-9-编程式路由导航" class="headerlink" title="4.9 编程式路由导航"></a>4.9 编程式路由导航</h2><p>作用：不借助 <code>&lt;router-link&gt;</code> 实现路由跳转，让路由跳转更灵活。</p>
<p><img src="https://img-blog.csdnimg.cn/9572985ce25c4f2698341d8008642c4d.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>相关的API：（所有组件共享$router）</p>
<table>
<thead>
<tr>
<th align="left">方法名</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">this.$router.push(path)</td>
<td align="left">相当于点击路由链接(可以返回到当前路由界面)</td>
</tr>
<tr>
<td align="left">this.$router.replace(path)</td>
<td align="left">用新路由替换当前路由(不可以返回到当前路由界面)</td>
</tr>
<tr>
<td align="left">this.$router.back()</td>
<td align="left">请求(返回)上一个记录路由</td>
</tr>
<tr>
<td align="left">this.$router.go(-1)</td>
<td align="left">请求(返回)上一个记录路由</td>
</tr>
<tr>
<td align="left">this.$router.go(1)</td>
<td align="left">请求下一个记录路由</td>
</tr>
</tbody></table>
<h2 id="4-10-缓存路由组件"><a href="#4-10-缓存路由组件" class="headerlink" title="4.10 缓存路由组件"></a>4.10 缓存路由组件</h2><p><strong>作用：</strong> 让不展示的路由组件保持挂载，不被销毁</p>
<p><strong>使用方法：</strong> 使用 keep-alive标签包裹 router-view</p>
<p>其中 include属性可以指定缓存的组件名，如果不指定则缓存显示过的路由组件。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- keep-alive 缓存指定的组件名 单个的写法--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">keep-alive</span> <span class="hljs-attr">include</span>=<span class="hljs-string">&quot;AboutNews&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">router-view</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-view</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">keep-alive</span>&gt;</span><br><span class="hljs-comment">&lt;!-- keep-alive 缓存指定的组件名 多个的写法--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">keep-alive</span> <span class="hljs-attr">:include</span>=<span class="hljs-string">&quot;[&#x27;AboutNews&#x27;]&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">router-view</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">router-view</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">keep-alive</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/90a8f4160a374a178467b2fe65d09d24.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-11-激活与失活钩子函数"><a href="#4-11-激活与失活钩子函数" class="headerlink" title="4.11 激活与失活钩子函数"></a>4.11 激活与失活钩子函数</h2><p>作用：路由组件所独有的两个钩子，用于捕获路由组件的激活状态。</p>
<p>具体名字：</p>
<ul>
<li>activated() 路由组件被激活时触发</li>
<li>deactivated() 路由组件失活时触发</li>
</ul>
<p>这两个钩子函数只在 <code>&lt;keep-alive&gt;</code> 标签内的路由视图有用，如果没有使用这个缓存的话，这两个钩子函数是没有效果的。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-title function_">activated</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">timer</span> = <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>       <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;News组件激活中&#x27;</span>)<br>    &#125;, <span class="hljs-number">16</span>)<br>&#125;,<br><span class="hljs-title function_">deactivated</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-built_in">clearInterval</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">timer</span>)<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;News组件已失活&#x27;</span>)<br>&#125;<br></code></pre></td></tr></table></figure>
<p><strong>效果图：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/be64b294be8b45bf9a7b57edf7b0e693.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-12-路由元信息"><a href="#4-12-路由元信息" class="headerlink" title="4.12 路由元信息"></a>4.12 路由元信息</h2><p>之前我们通过 name、path、component、query、params等参数指定了路由的信息，除此之外，它还有 <a target="_blank" rel="noopener" href="https://v3.router.vuejs.org/zh/guide/advanced/meta.html#%E8%B7%AF%E7%94%B1%E5%85%83%E4%BF%A1%E6%81%AF"><strong>meta</strong></a> 元信息。</p>
<blockquote>
<p>官方称 $routes 配置中的每个路由对象为 <strong>路由记录</strong>。</p>
</blockquote>
<p>meta 信息一般用于权限的验证或者数据校验。</p>
<p>可通过 <code>$route</code> 获取到meta信息，除此之外，<code>$route.matched</code>会包含当前路由与上级的所有路由，通常会通过遍历这个数组对meta进行权限验证，官方案例（登录校验）示例代码如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs js">router.<span class="hljs-title function_">beforeEach</span>(<span class="hljs-function">(<span class="hljs-params">to, <span class="hljs-keyword">from</span>, next</span>) =&gt;</span> &#123;<br>  <span class="hljs-comment">// 判断当前的路由加上父路由的meta标记的是否需要验证</span><br>  <span class="hljs-keyword">if</span> (to.<span class="hljs-property">matched</span>.<span class="hljs-title function_">some</span>(<span class="hljs-function"><span class="hljs-params">record</span> =&gt;</span> record.<span class="hljs-property">meta</span>.<span class="hljs-property">requiresAuth</span>)) &#123;<br>	<span class="hljs-comment">// 调用方法查看是否是登录用户</span><br>    <span class="hljs-keyword">if</span> (!auth.<span class="hljs-title function_">loggedIn</span>()) &#123;<br>      <span class="hljs-title function_">next</span>(&#123;	<span class="hljs-comment">// 若未登录，则重定向到登录页面</span><br>        <span class="hljs-attr">path</span>: <span class="hljs-string">&#x27;/login&#x27;</span>,<br>        <span class="hljs-attr">query</span>: &#123; <span class="hljs-attr">redirect</span>: to.<span class="hljs-property">fullPath</span> &#125;<br>      &#125;)<br>    &#125; <span class="hljs-keyword">else</span> &#123;    <span class="hljs-comment">// 若已登录，则通过</span><br>      <span class="hljs-title function_">next</span>()<br>    &#125;<br>  &#125; <span class="hljs-keyword">else</span> &#123;		<span class="hljs-comment">// 若无需验证，则直接通过</span><br>    <span class="hljs-title function_">next</span>() <br>  &#125;<br>&#125;)<br></code></pre></td></tr></table></figure>

<p>其中 beforeEach() 是路由守卫的一个钩子函数，接下来会做记录。</p>
<h2 id="4-13-路由导航守卫"><a href="#4-13-路由导航守卫" class="headerlink" title="4.13 路由导航守卫"></a>4.13 路由导航守卫</h2><p><a target="_blank" rel="noopener" href="https://v3.router.vuejs.org/zh/guide/advanced/navigation-guards.html#%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB"><strong>导航守卫</strong></a> </p>
<p>“导航”表示路由正在发生改变。</p>
<blockquote>
<p>参数或查询的改变并不会触发进入&#x2F;离开的导航守卫。你可以通过观察 $route 对象来应对这些变化，或使用 beforeRouteUpdate 的组件内守卫。</p>
</blockquote>
<p>导航守卫主要有以下5种：</p>
<ul>
<li>全局前置守卫 <strong>beforeEach((to, from, next) &#x3D;&gt; {})</strong></li>
<li>全局解析守卫 <strong>beforeResolve(((to, from, next) &#x3D;&gt; {})</strong> </li>
<li>全局后置钩子 <strong>afterEach((to, from) &#x3D;&gt; {})</strong></li>
<li>路由独享的守卫 <strong>beforeEnter((to, from, next)&#x3D;&gt;{})</strong></li>
<li>组件内的守卫 **(beforeRouteEnter，beforeRouteUpdate ，beforeRouteLeave)**，参数都是 (to, from, next) &#x3D;&gt; {}</li>
</ul>
<p>在使用这些守卫时，比较关键的就是要知道它们的形参里有哪些参数，参数分别代表什么含义，除了后置钩子只有 <code>to</code> 和 <code>from</code> 以外，其他守卫都有三种参数，分别是 <code>to、from</code> 和 <code>next</code></p>
<table>
<thead>
<tr>
<th>参数</th>
<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>to</td>
<td>Route</td>
<td>即将要进入的目标 路由对象</td>
</tr>
<tr>
<td>from</td>
<td>Route</td>
<td>当前导航正要离开的路由</td>
</tr>
<tr>
<td>next</td>
<td>Function</td>
<td>进行管道中的下一个钩子</td>
</tr>
</tbody></table>
<p>其中第一个参数 to 和 第二个参数 from 都是  Route类型，即之前输出的组件里的 <code>$route</code>，通过这两个参数可以获取到路由的信息，比如 name、meta、query、params等，通常都是获取 meta 信息进行权限验证。</p>
<p>关于第三个参数 next（除了afterEach()钩子以外，其他都有），常见用法有：</p>
<ul>
<li><p><strong>next()</strong>: 进行管道中的下一个钩子。如果全部钩子执行完了，则导航的状态就是 confirmed (确认的)。</p>
</li>
<li><p><strong>next(false)</strong>: 中断当前的导航。如果浏览器的 URL 改变了 (可能是用户手动或者浏览器后退按钮)，那么 URL 地址会重置到 from 路由对应的地址。</p>
</li>
<li><p><strong>next(‘&#x2F;‘)</strong> 、<strong>next({ path: ‘&#x2F;‘ })</strong> : 跳转到一个不同的地址。当前的导航被中断，然后进行一个新的导航。你可以向 next 传递任意位置对象，且允许设置诸如 <strong>replace: true、name: ‘home’</strong> 之类的选项以及任何用在 <strong>router-link</strong> 的 属性 或 <strong>router.push</strong> 中的选项。</p>
</li>
<li><p><strong>next(error)</strong>: 如果传入 next 的参数是一个 Error 实例，则导航会被终止且该错误会被传递给 router.onError() 注册过的回调。</p>
</li>
</ul>
<p><strong>执行流程：</strong></p>
<ol>
<li>导航被触发。</li>
<li>在失活的组件里调用 beforeRouteLeave 守卫。</li>
<li>调用全局的 beforeEach 守卫。</li>
<li>在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+)。</li>
<li>在路由配置里调用 beforeEnter。</li>
<li>解析异步路由组件。</li>
<li>在被激活的组件里调用 beforeRouteEnter。</li>
<li>调用全局的 beforeResolve 守卫 (2.5+)。</li>
<li>导航被确认。</li>
<li>调用全局的 afterEach 钩子。</li>
<li>触发 DOM 更新。</li>
<li>调用 beforeRouteEnter 守卫中传给 next 的回调函数，创建好的组件实例会作为回调函数的参数传入。</li>
</ol>
<p><strong>若只关注钩子函数的话，顺序是：</strong></p>
<p>① beforeRouteLeave<br>② beforeEach<br>③ beforeRouteUpdate<br>④ befreEnter<br>⑤ beforeRouteEnter<br>⑥ beforeResolve<br>⑦ affterEach<br>⑧ beforeRouteEnter </p>
<p><strong>翻译成中文的顺序：</strong></p>
<p>① 上一个组件的beforeRouteLeave 后卫<br>② 全局前置守卫<br>③ 当前组件的beforeRouteUpdate守卫<br>④ 路由独享的守卫<br>⑤ 当前组件的beforeRouteEnter 守卫<br>⑥ 全局解析守卫<br>⑦ 全局后置钩子<br>⑧ 当前组件的beforeRouteEnter守卫</p>
<p><strong>不考虑在组件设置守卫的顺序：</strong></p>
<p>① 全局前置守卫<br>② 路由独享的守卫<br>③ 全局解析守卫<br>④ 全局后置钩子</p>
<p><strong>只考虑在组件设置守卫的顺序：</strong></p>
<p>① 上一个组件的beforeRouteLeave 后卫<br>② 当前组件的beforeRouteUpdate守卫<br>③ 当前组件的beforeRouteEnter 守卫<br>④ 当前组件的beforeRouteEnter守卫</p>
<p><strong>总结：</strong></p>
<ul>
<li>全局守卫 <strong>beforeEach</strong> 、<strong>beforeResolve</strong> 和 <strong>afterEach</strong> 都是通过 VueRouter 对象进行定义的</li>
<li>在路由中定义的守卫只有 beforeEnter() 这个路由独享守卫</li>
<li>在路由组件中定义的有三种守卫beforeRouteEnter、beforeRouteUpdate、和beforeRouteLeave ，分别表示进入路由，更新路由时，离开路由前</li>
</ul>
<h2 id="4-14-路由器的工作模式"><a href="#4-14-路由器的工作模式" class="headerlink" title="4.14 路由器的工作模式"></a>4.14 路由器的工作模式</h2><p><strong>1. hash</strong></p>
<ul>
<li>地址中一直携带 <code>#</code> 符号，影响观感</li>
<li>若以后将地址通过第三方手机APP分享，如果APP校验严格，这种地址会被标记为非法地址。</li>
<li>兼容性比较好</li>
</ul>
<p><strong>2. history</strong> </p>
<ul>
<li>地址栏没有特殊的#</li>
<li>兼容性比hash差一些</li>
<li>应用部署上线后需要后端人员配合，解决刷新页面服务端404的问题（Node.js插件或Nginx反向代理）</li>
</ul>
<p><strong>使用方式：</strong></p>
<p>通过在VueRouter对象里配置mode属性来修改，默认是hash模式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span>(&#123;<br>	<span class="hljs-attr">mode</span>: hash,   <span class="hljs-comment">// 默认为hash </span><br>	<span class="hljs-attr">routes</span>: [...]<br>&#125;)<br></code></pre></td></tr></table></figure>
<h1 id="五、Vue2项目打包与部署"><a href="#五、Vue2项目打包与部署" class="headerlink" title="五、Vue2项目打包与部署"></a>五、Vue2项目打包与部署</h1><hr>
<p>第一步，打包Vue项目，需在项目路径里执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">npm run build<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/ba22eca6807b4a66b03381dcfae736d7.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>打包的结果会保存到当前目录下的 dist 文件夹里，是一些HTML、CSS、JS、图片等静态文件</p>
<p>接下来准备用node.js 进行部署</p>
<p>第二步，创建空的文件夹，执行终端：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">npm init <br></code></pre></td></tr></table></figure>
<p>然后根据提示取一个项目名称：</p>
<p><img src="https://img-blog.csdnimg.cn/37f78ca6b4e14eabb4fba24a14bfcaa6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>输入名称后多敲几次回车，后面的配置直接默认，创建好后，文件的目录如下：<br><img src="https://img-blog.csdnimg.cn/7d1edb743bf84851a063c8c07ec811fd.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>第三步，安装express （node.js进行web开发的一个框架）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">npm install express <br></code></pre></td></tr></table></figure>

<p>第四步，新建一个 <code>server.js</code>，编写服务器的运行脚本</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><br><span class="hljs-keyword">const</span> app = <span class="hljs-title function_">express</span>()<br><br>app.<span class="hljs-title function_">use</span>(express.<span class="hljs-title function_">static</span>(__dirname+<span class="hljs-string">&#x27;/static&#x27;</span>))<br>app.<span class="hljs-title function_">get</span>(<span class="hljs-string">&#x27;/person&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">req, resp</span>) =&gt;</span>&#123;<br>    resp.<span class="hljs-title function_">send</span>(&#123;<br>        <span class="hljs-attr">name</span>:<span class="hljs-string">&#x27;uni&#x27;</span>,<br>        <span class="hljs-attr">age</span>: <span class="hljs-number">22</span>,<br>    &#125;)<br>&#125;)<br><br>app.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8081</span>, <span class="hljs-function">(<span class="hljs-params">error</span>) =&gt;</span> &#123;<br>    <span class="hljs-keyword">if</span>(!error)<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;服务器启动成功!&#x27;</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>
<p>第五步，部署服务器</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">node server<br></code></pre></td></tr></table></figure>

<p>第六步，访问之前配置里写的接口&#x2F;person<br><img src="https://img-blog.csdnimg.cn/65a174f39eb24701ae88fdcc9815e40b.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>第七步，将之前Vue项目中打包的dist文件夹里的文件复制到当前项目的static文件夹下（没有则创建）<br><img src="https://img-blog.csdnimg.cn/b711b97ad7d54a33bf0da1766802144f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>第八步，在node.js部署终端输入 Ctrl + z 终止运行，重新进行部署<br><img src="https://img-blog.csdnimg.cn/224de4a83eff4ec185c657e65b33ad68.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>最后一步，访问部署的静态资源，默认是访问index.html 页面</p>
<p><img src="https://img-blog.csdnimg.cn/589b8502fa6f43569bde671300c66ab6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p><font color="red">注：目前这种部署方式只支持路由模式为hash的vue项目，否则在刷新页面时候会向服务器请求地址栏的资源，之前在脚手架中都是通过Vue的路由来进行SPA单页面局部渲染的，直接访问地址栏的资源会直接报错。</font></p>
<h2 id="5-1-Node-js库解决history模式路由的问题"><a href="#5-1-Node-js库解决history模式路由的问题" class="headerlink" title="5.1 Node.js库解决history模式路由的问题"></a>5.1 Node.js库解决history模式路由的问题</h2><p>Node.js插件 <a target="_blank" rel="noopener" href="https://www.npmjs.com/package/connect-history-api-fallback"><strong>connect-history-api-fallback v1.6.0</strong></a></p>
<p>最后更新时间 2019年</p>
<p>使用npm命令安装</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">npm i connect-history-api-fallback<br></code></pre></td></tr></table></figure>

<p>修改express的配置</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;express&#x27;</span>)<br><span class="hljs-keyword">const</span> hisotry = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;connect-history-api-fallback&#x27;</span>)<br><span class="hljs-keyword">const</span> app = <span class="hljs-title function_">express</span>()<br><br>app.<span class="hljs-title function_">use</span>(<span class="hljs-title function_">hisotry</span>())<br>app.<span class="hljs-title function_">use</span>(express.<span class="hljs-title function_">static</span>(__dirname+<span class="hljs-string">&#x27;/static&#x27;</span>))<br><br>app.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8081</span>, <span class="hljs-function">(<span class="hljs-params">error</span>) =&gt;</span> &#123;<br>    <span class="hljs-keyword">if</span>(!error)<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;服务器启动成功!&#x27;</span>)<br>&#125;)<br></code></pre></td></tr></table></figure>
<p>修改Vue2项目中路由的mode为history并导出：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> router = <span class="hljs-keyword">new</span> <span class="hljs-title class_">VueRouter</span>(&#123;<br>    <span class="hljs-attr">mode</span>: <span class="hljs-string">&#x27;history&#x27;</span>,<br>    ...<br>&#125;<br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> router<br></code></pre></td></tr></table></figure>

<p>还有另一种方法，通过 Ngnix 反向代理，这里使用node.js的插件比较容易解决history刷新页面报404的问题，用Ngnix的话得花时间学习一下它的安装与使用。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%89%8D%E7%AB%AF/" class="category-chain-item">前端</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Vue/">#Vue</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署</div>
      <div>http://example.com/2022/06/25/Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年6月25日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/04/Swagger%20%E7%AC%94%E8%AE%B0%20_%20Swagger%20%E7%AE%80%E4%BB%8B%20_%20Springfox%20%E7%AE%80%E4%BB%8B%20_%20Springfox%202.9.2%20%E5%B8%B8%E7%94%A8%E6%B3%A8%E8%A7%A3%20_%20Spring%20Boot%20%E6%95%B4%E5%90%88%20Swagger2%20%E6%A1%88%E4%BE%8B/" title="Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Swagger 笔记 _ Swagger 简介 _ Springfox 简介 _ Springfox 2.9.2 常用注解 _ Spring Boot 整合 Swagger2 案例</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/06/24/Vue2%20%E7%AC%94%E8%AE%B04%20%E5%9F%BA%E4%BA%8EVue2%E7%9A%84%E5%AD%A6%E7%94%9F%E4%BF%A1%E6%81%AF%E5%A2%9E%E5%88%A0%E6%9F%A5%E6%A1%88%E4%BE%8B%20_%20Vuex%E7%BB%84%E4%BB%B6%E7%AE%80%E4%BB%8B%E3%80%81%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%20_%20Vuex%E7%9A%84%E4%B8%89%E5%B1%82%E7%BB%93%E6%9E%84%E2%80%94%E2%80%94store%E8%B4%9F%E8%B4%A3%E5%93%8D%E5%BA%94%E7%BB%84%E4%BB%B6%E3%80%81mutations%E8%B4%9F%E8%B4%A3%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E3%80%81state%E8%B4%9F%E8%B4%A3%E5%AD%98%E5%82%A8%E6%95%B0%E6%8D%AE/" title="Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据">
                        <span class="hidden-mobile">Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
